package com.ray.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DutyBean;
import com.ray.bean.MaterialTechnologyBean;
import com.ray.bean.OrderBean;
import com.ray.bean.OrderInfoDetailBean;
import com.ray.bean.ReportBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.DepartmentMapper;
import com.ray.mapper.MaterialTypeTechnologyMapper;
import com.ray.mapper.OrderMapper;
import com.ray.pojo.Department;
import com.ray.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class MaterialTypeTechnologyService {
	
	@Autowired
	MaterialTypeTechnologyMapper materialTypeTechnologyMapper;

	
	/**
	 * 获取公司物料工艺
	 * @param status(状态  0使用  2禁用)
	 * @return
	 */
	public DatatablesViewPageBean getCompanyMaterialTechnology(Map map) {
		StringUtil.checkNotNull(map,"status","未获取到状态");
		int count=0;
		DatatablesViewPageBean<MaterialTechnologyBean> returnBean=new DatatablesViewPageBean<MaterialTechnologyBean>();
		returnBean.setReturnData(materialTypeTechnologyMapper.getCompanyMaterialTechnology(map));
		count=materialTypeTechnologyMapper.getCompanyMaterialTechnologyCount(map);
		returnBean.setiTotalDisplayRecords(count);
		returnBean.setiTotalRecords(count);
		return returnBean;
	}


	
	/**
	 * 添加物料工艺
	 * @param materialType(物料类型)
	 * @param technology(工艺)
	 * @return
	 */
	public void addMaterialTechnology(Map map) {
		StringUtil.checkIsTrue(map,"materialType","未获取到物料类型");
		StringUtil.checkIsTrue(map,"technology","未获取到工艺");
		this.checkMaterialTechnology(map);
		map.put("id", StringUtil.getUUID());
		materialTypeTechnologyMapper.addMaterialTechnology(map);
	}


	/**
	 * 校验物料工艺是否存在
	 * @param materialType(物料类型)
	 * @param technology(工艺)
	 * @param loginCompany(公司)
	 */
	private void checkMaterialTechnology(Map map) {
		 List<MaterialTechnologyBean> mateList=materialTypeTechnologyMapper.getCompanyMaterialTechnology(map);
		 if(mateList!=null && mateList.size()>0) {
			 MaterialTechnologyBean materialTechnologyBean=mateList.get(0);
			 Assert.isTrue(materialTechnologyBean.getStatus()!=0,"该物料工艺已存在");
			 Assert.isTrue(materialTechnologyBean.getStatus()!=2,"该物料工艺已存在但被禁用");
	     }
	}


	/**
	 * 设置物料工艺状态
	 * @param id(id)
	 * @param status(状态)
	 * @return
	 */
	public void setStatus(Map map) {
		StringUtil.checkIsTrue(map,"id","未获取到id");
		StringUtil.checkIsTrue(map,"status","未获取到状态");
		MaterialTechnologyBean materialTechnologyBean=materialTypeTechnologyMapper.getMaterialTechnologyBeanById(map.get("id").toString());
		Assert.notNull(materialTechnologyBean,"改物料工艺不存在");
		materialTypeTechnologyMapper.setStatus(map);
	}



	
	/**
	 * 修改物料工艺
	 * @param id(id)
	 * @param materialType(物料类型)
	 * @param technology(工艺)
	 * @return
	 */
	public void updateMaterialTechnology(Map map) {
		StringUtil.checkIsTrue(map,"id","未获取到id");
		StringUtil.checkIsTrue(map,"materialType","未获取到物料类型");
		StringUtil.checkIsTrue(map,"technology","未获取到工艺");
		this.checkMaterialTechnology(map);
		materialTypeTechnologyMapper.updateMaterialTechnology(map);
	}


	/**
	 * 从订单明细拓展表中获取订单明细信息
	 * @param orderNo(系统订单编号)
	 * @param lineNo(行号)
	 * @param materialFactory(仓位)
	 * @param materialType(物料类型)
	 * @return
	 */
	public List<OrderInfoDetailBean> getOrderDatailFromExtend(Map map) {
		StringUtil.checkIsTrue(map,"orderNo","未获取到订单编号");
		StringUtil.checkNotNull(map,"lineNo","未获取到行号");
		StringUtil.checkNotNull(map,"materialFactory","未获取到仓位");
		StringUtil.checkNotNull(map,"materialType","未获取到物料类型");
		return materialTypeTechnologyMapper.getOrderDatailFromExtend(map);
	}


	/**
	 * 设置订单明细拓展表信息
	 * @param jsonArray(json数组字符串)
	 * @param orderNo(系统订单编号  必填)
	 * @param lineNo(行号 必填)
	 * @param materialFactory(仓位 必填)
	 * @param materialType(物料类型 必填)
	 * @param arts(物料类型 根据配置填不填)
	 * @param reportCount(订单实际可报工数)
	 * @return
	 */
	public void setOrderDatailExtendDetail(Map map) {
		StringUtil.checkIsTrue(map,"jsonArray","未获取到参数");
		String jsonArray=map.get("jsonArray").toString();
		JSONArray jsonArrays=JSONArray.fromObject(jsonArray);
		List<OrderInfoDetailBean> detailList=(List<OrderInfoDetailBean>) JSONArray.toCollection(jsonArrays, OrderInfoDetailBean.class);
	    Assert.isTrue(detailList!=null && detailList.size()>0,"参数值长度为0");
	    int row=materialTypeTechnologyMapper.deleteOrderDatailExtendByDetailList(detailList);
	    int insertRow=materialTypeTechnologyMapper.insertOrderDatailExtendByDetailList(detailList);
	}

    

	

}
